package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.service.impl.DishServiceImpl;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("添加菜品")
    public Result add(@RequestBody DishDTO dto){
        log.info("添加菜品");
        dishService.add(dto);
        //清除原先的缓存，重新加载
        //精确清理，根据key来清理
        String key="dish_"+dto.getCategoryId();
        redisTemplate.delete(key);

        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult>page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询");
        PageResult pageResult=dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 菜品的起售和禁售
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品的起售和禁售")
    public Result<Object> startOrStop(@PathVariable() Long status,Integer id){
        log.info("起售和禁售");
        dishService.startOrStop(status,id);


        //批量删除       清理缓存，这里ids是个列表，我们可以通过批量清理所有以dish_开头的key
        Set keys = redisTemplate.keys("dish_*");//类似于keys *
        redisTemplate.delete(keys);

        return Result.success();
    }

    /**修改菜品
     *
     */
    @PutMapping()
    public Result<Object>update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品");
        dishService.updateDishWithFlavor(dishDTO);
        //因为修改菜品可能会涉及到修改类别，所以还是全删了比较好
        //批量删除       清理缓存，这里ids是个列表，我们可以通过批量清理所有以dish_开头的key
        Set keys = redisTemplate.keys("dish_*");//类似于keys *
        redisTemplate.delete(keys);


        return Result.success();
    }

    /**
     * 根据id查询菜品和口味
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品和口味")
    public Result<DishVO> getByIdWithFlavor(@PathVariable Long id){
        log.info("根据id查询菜品和口味");
        DishVO dishVO=dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 删除菜品
     */
    @DeleteMapping()
    @ApiOperation("删除菜品")
    public Result delete(@RequestParam List<Long> ids){
        dishService.delete(ids);

        //批量删除       清理缓存，这里ids是个列表，我们可以通过批量清理所有以dish_开头的key
        Set keys = redisTemplate.keys("dish_*");//类似于keys *
        redisTemplate.delete(keys);

        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> listById(Integer categoryId) {
        log.info("根据分类id查询信息");
        List<Dish>list=dishService.listById(categoryId);
        return Result.success(list);
    }
}
